using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using PoserLoot.Web.Models;
using PoserLoot.Web.Core;
using PoserLoot.Web.Core.Paging;

namespace PoserLoot.Web.Controllers
{
    public class EntryController : BaseController
    {
        public ActionResult Index(Guid? id, int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;

            ViewData["IEnumerable<Category>"] = _service.ListCategories();

            ViewData["Page.Title"] = "All Entries";
            ViewData["CategoryId"] = Guid.Empty;
            if (id.HasValue)
            {
                ViewData["CategoryId"] = id.Value;            
                ViewData["Page.Title"] = _service.GetCategory(id.Value).Appellation;
            }

            var q = _service.ListEntries(id).ToPagedList(currentPageIndex, pageSize);

            return View(q);
        }

        public ActionResult Search(FormCollection collection, int? page)
        {
            string keyword = collection["Keyword"];
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var q = _service.ListEntries(keyword).ToPagedList(currentPageIndex, pageSize);

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            ViewData["Keyword"] = keyword;
            ViewData["CategoryId"] = Guid.Empty;
            ViewData["Page.Title"] = "Search";

            return View("Index", q);
        }

        public ActionResult Details(Guid id)
        {
            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            var entry = _service.GetEntry(id);
            entry.CategoryReference.Load();
            ViewData["IEnumerable<Gallery>"] = _service.ListGallerys(id);
            ViewData["IEnumerable<File>"] = _service.ListFiles(id);
            ViewData["IEnumerable<Comment>"] = _service.ListComments(id);

            return View(entry);
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Create(Guid id)
        {
            ViewData["category.Id"] = id;

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View();
        }

        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Guid id, FormCollection collection)
        {
            Entry entry = new Entry()
            {
                Code = collection["Code"],
                Appellation = collection["Appellation"],
                Summary = collection["Summary"],
                Description = collection["Description"],
                ReferenceUrl = collection["ReferenceUrl"],
                Category = _service.GetCategory(id)
            };

            if (_service.CreateEntry(entry))
                return RedirectToAction("Details", new { id = entry.EntryId });

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View();
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(Guid id)
        {
            var entry = _service.GetEntry(id);

            if (!entry.CategoryReference.IsLoaded)
                entry.CategoryReference.Load();

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View(entry);
        }

        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            Entry entry = _service.GetEntry(id);
            entry.Code = collection["Code"];
            entry.Appellation = collection["Appellation"];
            entry.Summary = collection["Summary"];
            entry.Description = collection["Description"];
            entry.ReferenceUrl = collection["ReferenceUrl"];

            if (_service.EditEntry(entry))
                return RedirectToAction("Details", new { id = entry.EntryId });

            if (!entry.CategoryReference.IsLoaded)
                entry.CategoryReference.Load();

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View(entry);
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult SetThumbnail(Guid id, Guid galleryId)
        {
            Entry entry = _service.GetEntry(id);
            entry.Thumbnail = _service.GetGallery(galleryId);

            if (_service.EditEntry(entry))
                return RedirectToAction("Details", new { id = entry.EntryId });

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            
            return View(entry);
        }
    }
}
